﻿namespace Harness.Commands
{
    using System;
    using System.Collections.Generic;
    using System.Drawing.Imaging;
    using System.Linq;

    internal class ScreenshotCommand : ICommand
    {
        #region Constants

        private const int ActionArgIndex = 0;
        private const int FilenameArgIndex = 1;
 
        #endregion

        #region Public Methods

        public void SyntaxCheck(IHarness harness, IEnumerable<object> args)
        {
            string action = (args.ElementAtOrDefault(ActionArgIndex) ?? string.Empty).ToString().ToLower();

            switch (action)
            {
                case "save":
                    if (null == args.ElementAtOrDefault(FilenameArgIndex))
                    {
                        throw new ArgumentNullException((FilenameArgIndex + 1).ToString() + ":Filename");
                    }

                    break;

                case "copy":
                case "get":
                    break;

                default:
                    throw new ArgumentException((ActionArgIndex + 1).ToString() + ":Action(save/copy/get)");
            }
        }

        public dynamic Execute(IHarness harness, IEnumerable<object> args)
        {
            string action = (args.ElementAtOrDefault(ActionArgIndex) ?? string.Empty).ToString().ToLower();

            switch (action)
            {
                case "save":
                    string filename = args.ElementAtOrDefault(FilenameArgIndex) as string;
                    IE.SaveScreenshot(filename);
                    break;

                case "copy":
                    IE.CopyScreenshot();
                    break;

                case "get":
                    return IE.GetScreenshot();
            }

            return null;
        }

        #endregion
    }
}
